package muview;

import com.cloudgarden.resource.SWTResourceManager;
import java.io.File;
import java.io.IOException;

import mucontrol.*;
import mumethod.*;
import muobject.SystemProperty;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class LoginPage {
	
	private Button btn_tlogin;
	private Button but_slogin;
	private Button but_databasepath;
	private Label lab_password;
	private Label lab_account;
	private Label lab_message;
	private Label lab_dbmessage;
	private Text txt_password;
	private Text txt_account;
	
	private String APP_TITLE = "活力恆動一二三";
	
	private ApplicationContext springConnector;
	private boolean dbConnectFlag;
	private Shell shell = null;
	private String rootPath;
	private String imagePath;
	private String templatePath;
	private String dbPath;
	private PropertiesFunction propertiesFunction;
	private SystemProperty systemProperty;
	
	private LoginControl loginControl;
	
	public static void main(String[] args) throws IOException {
		System.setProperty("file.encoding", "big5");
		org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
		LoginPage loginControl = new LoginPage();
		loginControl.Init();
		loginControl.createContent();
		loginControl.shell.open();

		while (!loginControl.shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private void Init() {
		try {
			propertiesFunction = new PropertiesFunction();
			rootPath = new File(".").getAbsolutePath();
			dbPath = propertiesFunction.ReadProperties("database.location");
			imagePath = new File(".").getCanonicalPath() + "\\image\\";
			templatePath = new File(".").getCanonicalPath() + "\\template\\";
			if (ValidDBConnect.ConnectTest(dbPath, "Users")){
				propertiesFunction.WriteProperties("database.location", dbPath);
				dbConnectFlag = true;
				springConnector = new ClassPathXmlApplicationContext("muproject.xml");
				loginControl = (LoginControl) springConnector.getBean("LoginControlService");
			}else
				dbConnectFlag = false;
		} catch (IOException e) {
			dbConnectFlag = false;
		}
	}
	
	private void Reset(String path){
		try {
			propertiesFunction = new PropertiesFunction();
			rootPath = new File(".").getAbsolutePath();
			dbPath = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + path;
			imagePath = new File(".").getCanonicalPath() + "\\image\\";
			templatePath = new File(".").getCanonicalPath() + "\\template\\";
			if (ValidDBConnect.ConnectTest(dbPath, "Users")){
				propertiesFunction.WriteProperties("database.location", dbPath);
				dbConnectFlag = true;
				springConnector = new ClassPathXmlApplicationContext("muproject.xml");
				loginControl = (LoginControl) springConnector.getBean("LoginControlService");
			}else
				dbConnectFlag = false;
		} catch (IOException e) {
			dbConnectFlag = false;
		}
	}

	private void createContent() throws IOException {
		shell = new org.eclipse.swt.widgets.Shell(SWT.SHELL_TRIM & (~SWT.RESIZE) & (~SWT.MAX));

		{
			SWTResourceManager.registerResourceUser(shell);
		}
		
		shell.setText(APP_TITLE);
		shell.setLayout(new FormLayout());
		shell.setSize(800, 600);
		shell.setImage(new Image(shell.getDisplay(), imagePath + "login.ico"));
		ImageData data = new ImageData(imagePath + "login_background.jpg");
		data.scaledTo(shell.getBounds().width, shell.getBounds().height);
		Image image = new Image(shell.getDisplay(), data);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setBackgroundImage(image);
		{
			FormData lab_dbmessageLData = new FormData();
			lab_dbmessageLData.left =  new FormAttachment(0, 1000, 12);
			lab_dbmessageLData.top =  new FormAttachment(0, 1000, 11);
			lab_dbmessageLData.width = 323;
			lab_dbmessageLData.height = 28;
			lab_dbmessage = new Label(shell, SWT.NONE);
			lab_dbmessage.setLayoutData(lab_dbmessageLData);
			lab_dbmessage.setFont(SWTResourceManager.getFont("Tahoma", 14, 1, false, false));
			if (!dbConnectFlag){
				lab_dbmessage.setForeground(new Color(shell.getDisplay(), 255, 0, 0));
				lab_dbmessage.setText("連接資料庫失敗, 請重新連接!");}
			else{
				lab_dbmessage.setForeground(new Color(shell.getDisplay(), 110, 123, 139));
				lab_dbmessage.setText("已連接資料庫!");}
		}
		{
			but_databasepath = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData but_databasepathLData = new FormData();
			but_databasepathLData.left =  new FormAttachment(0, 1000, 13);
			but_databasepathLData.top =  new FormAttachment(0, 1000, 42);
			but_databasepathLData.width = 72;
			but_databasepathLData.height = 23;
			but_databasepath.setLayoutData(but_databasepathLData);
			but_databasepath.setText("\u8cc7\u6599\u5eab\u8a2d\u5b9a");
			but_databasepath.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					DialogDatabasePath dialogDatabasePathnew = new DialogDatabasePath(shell);
					String resetPath = dialogDatabasePathnew.open();
					if (resetPath != "")
						Reset(resetPath);
					if (!dbConnectFlag){
						lab_dbmessage.setForeground(new Color(shell.getDisplay(), 255, 0, 0));
						lab_dbmessage.setText("連接資料庫失敗, 請重新連接!");}
					else{
						lab_dbmessage.setForeground(new Color(shell.getDisplay(), 110, 123, 139));
						lab_dbmessage.setText("已連接資料庫!");}
				}
			});
		}
		{
			lab_password = new Label(shell, SWT.NONE);
			FormData lab_passwordLData = new FormData();
			lab_passwordLData.left =  new FormAttachment(0, 1000, 110);
			lab_passwordLData.top =  new FormAttachment(0, 1000, 511);
			lab_passwordLData.width = 148;
			lab_passwordLData.height = 30;
			lab_password.setLayoutData(lab_passwordLData);
			lab_password.setText("\u4f7f\u7528\u8005\u5bc6\u78bc :");
			lab_password.setFont(SWTResourceManager.getFont("Tahoma", 18, 1, false, false));
			lab_password.setForeground(new Color(shell.getDisplay(), 255, 255, 255));
		}
		{
			lab_account = new Label(shell, SWT.NONE);
			FormData lab_accountLData = new FormData();
			lab_accountLData.left =  new FormAttachment(0, 1000, 110);
			lab_accountLData.top =  new FormAttachment(0, 1000, 475);
			lab_accountLData.width = 148;
			lab_accountLData.height = 30;
			lab_account.setLayoutData(lab_accountLData);
			lab_account.setText("\u4f7f\u7528\u8005\u5e33\u865f :");
			lab_account.setFont(SWTResourceManager.getFont("Tahoma", 18, 1, false, false));
			lab_account.setForeground(new Color(shell.getDisplay(), 255, 255, 255));
		}
		{
			but_slogin = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData but_sloginLData = new FormData();
			but_sloginLData.left =  new FormAttachment(0, 1000, 593);
			but_sloginLData.top =  new FormAttachment(0, 1000, 476);
			but_sloginLData.width = 125;
			but_sloginLData.height = 70;
			but_slogin.setLayoutData(but_sloginLData);
			but_slogin.setText("\u5b78\u751f\u767b\u5165");
			but_slogin.setFont(SWTResourceManager.getFont("Tahoma", 12, 1,
					false, false));
			but_slogin.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					systemProperty = new SystemProperty(springConnector, rootPath, imagePath, templatePath, dbPath, "", "", "", "");
					shell.dispose();
					TeacherPage.disply(systemProperty);
				}
			});
		}
		{
			FormData lab_messageLData = new FormData();
			lab_messageLData.left =  new FormAttachment(0, 1000, 258);
			lab_messageLData.top =  new FormAttachment(0, 1000, 541);
			lab_messageLData.width = 200;
			lab_messageLData.height = 25;
			lab_message = new Label(shell, SWT.NONE);
			lab_message
					.setForeground(new Color(shell.getDisplay(), 255, 0, 0));
			lab_message.setLayoutData(lab_messageLData);
			lab_message.setFont(SWTResourceManager.getFont("Tahoma", 14, 1, false, false));
		}
		{
			FormData txt_passwordLData = new FormData();
			txt_passwordLData.left =  new FormAttachment(0, 1000, 258);
			txt_passwordLData.top =  new FormAttachment(0, 1000, 516);
			txt_passwordLData.width = 168;
			txt_passwordLData.height = 19;
			txt_password = new Text(shell, SWT.PASSWORD | SWT.BORDER);
			txt_password.setLayoutData(txt_passwordLData);
		}
		{
			FormData txt_accountLData = new FormData();
			txt_accountLData.left =  new FormAttachment(0, 1000, 258);
			txt_accountLData.top =  new FormAttachment(0, 1000, 479);
			txt_accountLData.width = 168;
			txt_accountLData.height = 19;
			txt_account = new Text(shell, SWT.BORDER);
			txt_account.setLayoutData(txt_accountLData);
		}
		{
			btn_tlogin = new Button(shell, SWT.PUSH | SWT.CENTER);
			FormData btn_loginLData = new FormData();
			btn_loginLData.left =  new FormAttachment(0, 1000, 456);
			btn_loginLData.top =  new FormAttachment(0, 1000, 476);
			btn_loginLData.width = 125;
			btn_loginLData.height = 70;
			btn_tlogin.setLayoutData(btn_loginLData);
			btn_tlogin.setText("\u8001\u5e2b\u767b\u5165");
			btn_tlogin.setFont(SWTResourceManager.getFont("Tahoma", 12, 1,
					false, false));
			btn_tlogin.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					systemProperty = new SystemProperty(springConnector, rootPath, imagePath, templatePath, dbPath, "", "", "", "");
					if (loginControl.checkTeacherLogin(txt_account.getText(), txt_password.getText(), "T")) {
						shell.dispose();
						TeacherPage.disply(systemProperty);
					} else {
						lab_message.setText("登入失敗!!");
					}
					/*shell.dispose();
					TeacherPage.disply(systemProperty);*/
					//MainPage.disply();
				}
			});
		}
		
		Control[] controls = new Control[] { txt_account, txt_password, btn_tlogin, but_slogin };
		shell.setTabList(controls);
	}
}
